﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Workbench.Models;
using SharedGenomics.Workbench.Views;
using SharedGenomics.Core;
using SharedGenomics.Domain;

namespace SharedGenomics.Workbench.Presenters
{
    public class DataFilterResultsPresenter : Presenter
    {
        public DataFilterResultsPresenter(IDataFilterResultsView view, DataFilterResultsModel model, WatchListPresenter watchListPresenter)
            : base(view)
        {
            //set initial view state
            view.ShowSnps = model.ShowSnps;
            view.ShowIndividuals = model.ShowIndividuals;

            //register for view events
            view.ShowSnpsChanged += new EventHandler(this.View_ShowSnpsChanged);
            view.ShowIndividualsChanged += new EventHandler(this.View_ShowIndividualsChanged);

            //register for watch list events
            watchListPresenter.WatchListLoaded += (o, e) => { this.SyncWatchListWithResults(); };
            watchListPresenter.SelectedItemChanged += new EventHandler(this.WatchListPresenter_SelectedItemChanged);

            //register for model events
            model.ListChanged += (o, e) => { this.SyncWatchListWithResults(); };

            //set model on view
            view.Model = model;

            //set private properties
            this.ResultsView = view;
            this.Model = model;
            this.WatchListPresenter = watchListPresenter;
        }

        public void FilterResultsAvailable(FilterResult filterResult)
        {
            filterResult.ThrowIfNull("filterResult");

            //this.Model.Clear();
            this.ResultsView.RemainingSnpCount = filterResult.RemainingSnpCount.ToString();
            this.ResultsView.RemainingIndividualCount = filterResult.RemainingIndividualCount.ToString();
            this.Model.LoadItems(filterResult.FilteredEntities);
        }

        public void ClearResults()
        {
            this.Model.Clear();
            this.ResultsView.RemainingIndividualCount = String.Empty;
            this.ResultsView.RemainingSnpCount = String.Empty;
        }

        private void SyncWatchListWithResults()
        {
            //sync with all results?
            this.WatchListPresenter.SyncWith<EntityFilterSummary>(this.Model.AllResults, efs => efs.Id);
        }

        private void WatchListPresenter_SelectedItemChanged(object sender, EventArgs e)
        {
            EntityFilterSummary selectedSummary = this.Model.GetFilterSummaryById(this.WatchListPresenter.SelectedItem);

            //summary is null if it was not found in the model - if it was not found clear the filter reason textboxes, otherwise display removal info
            this.ResultsView.RemovalReason = selectedSummary == null ? String.Empty : selectedSummary.Reason.ToString();
            this.ResultsView.RemovalValue = selectedSummary == null ? String.Empty : selectedSummary.Value;
        }

        private void View_ShowIndividualsChanged(object sender, EventArgs e)
        {
            this.Model.ShowIndividuals = this.ResultsView.ShowIndividuals;
        }

        private void View_ShowSnpsChanged(object sender, EventArgs e)
        {
            this.Model.ShowSnps = this.ResultsView.ShowSnps;
        }

        private IDataFilterResultsView ResultsView { get; set; }
        private DataFilterResultsModel Model { get; set; }

        [Region("WatchList")]
        public WatchListPresenter WatchListPresenter { get; private set; }
    }
}
